package logic

import (
	"common/model"
	"context"
	"errors"
	user "user_srv/proto"
)

func (u *UserServer) CouponList(ctx context.Context, req *user.CouponListRequest) (*user.CouponListResponse, error) {
	var userinfo model.User
	err := userinfo.GetUserInfoById(int(req.UserId))
	if err != nil {
		return nil, err
	}
	if userinfo.ID == 0 {
		return nil, errors.New("请重新登录")
	}
	var coupons model.Coupon
	res, err := coupons.GetCouponInfoByCanBeClaimedLever(userinfo.Level)
	if err != nil {
		return nil, err
	}
	if len(res) == 0 {
		return nil, errors.New("暂时没有可领取的优惠券")
	}
	var CouponList []*user.CouponListInfo
	for _, v := range res {
		CouponList = append(CouponList, &user.CouponListInfo{
			CouponName:        v.CouponName,
			CouponPrice:       float32(v.CouponPrice),
			HowMoneyAvailable: float32(v.HowMoneyAvailable),
		})
	}
	return &user.CouponListResponse{AllCouponList: CouponList}, nil
}
